<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/1/21
 * Time: 11:10
 */

namespace App\Http\Controllers\MiniProgram;

use App\Http\Controllers\BaseController;
use App\Http\Logics\MiniProgram\OrderLogic;
use App\Http\Traits\OrderTrait;

class OrderController extends BaseController
{
    use OrderTrait;

    /**
     * @SWG\Post(
     *     path="/order/create",
     *     tags={"Order"},
     *     description="创建订单",
     *     operationId="createOrder",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="loginSession",
     *         in="query",
     *         description="验证秘钥",
     *         required=true,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="cart_id_str",
     *         in="query",
     *         description="购物车ID字符串",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="pet response"
     *     )
     * )
     */
    public function createOrder()
    {
        $loginSession = $this->loginSession;
        $cart_id_str = $this->cart_id_str;
        $data = [
            'loginSession' => $loginSession,
            'cart_id_str' => $cart_id_str
        ];
        $response = OrderLogic::createOrder($data);
        if ($response->code){
            return $this->response($response->code, $response->message);
        }
        return $this->response($response->code, $response->message, $response->data);
    }

    /**
     * @SWG\Post(
     *     path="/order/detail",
     *     tags={"Order"},
     *     description="订单详情",
     *     operationId="getOrderDetail",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="loginSession",
     *         in="query",
     *         description="验证秘钥",
     *         required=true,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="order_id",
     *         in="query",
     *         description="订单ID",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="pet response"
     *     )
     * )
     */
    public function getOrderDetail()
    {
        $loginSession = $this->loginSession;
        $order_id = $this->order_id;
        $data = [
            'loginSession' => $loginSession,
            'order_id' => $order_id
        ];
        $response = OrderLogic::getOrderDetail($data);
        if ($response->code){
            return $this->response($response->code, $response->message);
        }
        return $this->response($response->code, $response->message, $response->data);
    }

    /**
     * @SWG\Post(
     *     path="/order/pay",
     *     tags={"Order"},
     *     description="订单支付",
     *     operationId="pay",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="loginSession",
     *         in="query",
     *         description="验证秘钥",
     *         required=true,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="order_id",
     *         in="query",
     *         description="订单ID",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="address_id",
     *         in="query",
     *         description="地址ID",
     *         required=false,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="pet response"
     *     )
     * )
     */
    public function pay()
    {
        $loginSession = $this->loginSession;
        $order_id = $this->order_id;
        $address_id = $this->address_id;
        $data = [
            'loginSession' => $loginSession,
            'order_id' => $order_id,
            'address_id' => $address_id
        ];
        $response = OrderLogic::pay($data);
        if ($response->code){
            return $this->response($response->code, $response->message);
        }
        return $this->response($response->code, $response->message, $response->data);
    }

    /**
     * @SWG\Post(
     *     path="/order/paySuccess",
     *     tags={"Order"},
     *     description="支付成功回调函数",
     *     operationId="paySuccess",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="loginSession",
     *         in="query",
     *         description="验证秘钥",
     *         required=true,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="order_id",
     *         in="query",
     *         description="订单ID",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="pet response"
     *     )
     * )
     */
    public function paySuccess()
    {
        $order_id = $this->order_id;
        $data = [
            'order_id' => $order_id
        ];
        $response = OrderLogic::paySuccess($data);
        if ($response->code){
            return $this->response($response->code, $response->message);
        }
        return $this->response($response->code, $response->message, $response->data);
    }

    /**
     * 小程序支付异步回调接口
     */
    public function callback()
    {
        $post_data = xml2Array(postData());
        OrderLogic::callback($post_data);
    }

    /**
     * @SWG\Get(
     *     path="/order/list",
     *     tags={"Order"},
     *     description="获取订单列表",
     *     operationId="getOrderList",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="loginSession",
     *         in="query",
     *         description="验证秘钥",
     *         required=true,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="status",
     *         in="query",
     *         description="订单状态",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="limit",
     *         in="query",
     *         description="分页大小",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="pet response"
     *     )
     * )
     */
    public function getOrderList()
    {
        $loginSession = $this->loginSession;
        $status = $this->status;
        $limit = $this->limit;
        $data = [
            'loginSession' => $loginSession,
            'status' => $status,
            'limit' => $limit
        ];
        $response = OrderLogic::getOrderList($data);
        if ($response->code){
            return $this->response($response->code, $response->message);
        }
        return $this->response($response->code, $response->message, $response->data);
    }

    /**
     * @SWG\Get(
     *     path="/order/wlcx",
     *     tags={"Order"},
     *     description="物流查询",
     *     operationId="wlxx",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="loginSession",
     *         in="query",
     *         description="验证秘钥",
     *         required=true,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="order_id",
     *         in="query",
     *         description="订单ID",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="pet response"
     *     )
     * )
     */
    public function wlcx()
    {
        $order_id = $this->order_id;
        $data = [
            'order_id' => $order_id
        ];
        $response = OrderLogic::wlcx($data);
        if ($response->code){
            return $this->response($response->code, $response->message);
        }
        return $this->response($response->code, $response->message, $response->data);
    }

    /**
     * @SWG\Post(
     *     path="/order/confirmReceive",
     *     tags={"Order"},
     *     description="确认收货",
     *     operationId="confirmReceive",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="loginSession",
     *         in="query",
     *         description="验证秘钥",
     *         required=true,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="order_id",
     *         in="query",
     *         description="订单ID",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="pet response"
     *     )
     * )
     */
    public function confirmReceive()
    {
        $order_id = $this->order_id;
        $data = [
            'order_id' => $order_id
        ];
        $response = OrderLogic::confirmReceive($data);
        if ($response->code){
            return $this->response($response->code, $response->message);
        }
        return $this->response($response->code, $response->message, $response->data);
    }


    /**
     * @SWG\Post(
     *     path="/order/directBuy",
     *     tags={"Order"},
     *     description="直接购买",
     *     operationId="directBuy",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="loginSession",
     *         in="query",
     *         description="验证秘钥",
     *         required=true,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="goods_id",
     *         in="query",
     *         description="商品ID",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="num",
     *         in="query",
     *         description="购买数量",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="pet response"
     *     )
     * )
     */
    public function directBuy()
    {
        $loginSession = $this->loginSession;
        $goods_id = $this->goods_id;
        $num = $this->num;
        $data = [
            'loginSession' => $loginSession,
            'goods_id' => $goods_id,
            'num' => $num
        ];
        $response = OrderLogic::directBuy($data);
        if ($response->code){
            return $this->response($response->code, $response->message);
        }
        return $this->response($response->code, $response->message, $response->data);
    }

    /**
     * @SWG\Post(
     *     path="/order/cancel",
     *     tags={"Order"},
     *     description="取消订单",
     *     operationId="cancelOrder",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="loginSession",
     *         in="query",
     *         description="验证秘钥",
     *         required=true,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="order_id",
     *         in="query",
     *         description="订单ID",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="pet response"
     *     )
     * )
     */
    public function cancelOrder()
    {
        $loginSession = $this->loginSession;
        $order_id = $this->order_id;
        $data = [
            'loginSession' => $loginSession,
            'order_id' => $order_id
        ];
        $response = OrderLogic::cancelOrder($data);
        if ($response->code){
            return $this->response($response->code, $response->message);
        }
        return $this->response($response->code, $response->message, $response->data);
    }
}